clear all;close all;clc
% x_loc = 0:-5:-355;
x_loc = [0:-5:-175 180:-5:5];
y_loc = [-57,-30,-15,0,15,30,45,60,75];

L_or_R = 1;
soft_hard = 0; % this is the hard mask!!!
rad = 0.3;

ifplot=0;
for Condition = 1:6

    if Condition==1
        tar_index = 13; % target is on the left, -60
        noise_index = 61; % noise on the right, +60; we only need to run the model for +60 to get rid of noise!
    elseif Condition==2
        tar_index = 4; % target is on the left, -15
        noise_index = 70; % noise on the right, +15; we only need to run the model for +60 to get rid of noise!
    elseif Condition==3
        tar_index = 1; % target is on the front center 0,
        noise_index = 72; % noise on the right, +5;
    elseif Condition==4
        tar_index = 56; % target is on the right +85,
        noise_index = 55; % noise on the right, +90;  Very bad, but still need to do it and grade.
    elseif Condition==5
        % % % % % % % tar_index = 58; % target is on the right +75,
        % % % % % % % noise_index = 52; % noise on the right, +105;
        tar_index = 60; % target is on the right +65,
        noise_index = 55; % noise on the right, +90;
    elseif Condition==6
        tar_index = 37; % target is on the back +180,
        noise_index = 38; % noise on the back, +175;
    end


    Elevation = 4; % ear level, 4
    x_loc(tar_index)
    x_loc(noise_index)

    fs = 32.1e3; % this is the one for the 3D3A database
    % out_hart = ncread('Acoustic/Subject1/Subject1_HRIRs.sofa',"Data.IR");
    out_hart = ncread('Acoustic/Subject15/Subject15_HRIRs.sofa',"Data.IR");
    % for igroup = 1:2 % 2 groups will make 200 sentences
    for igroup = 3:8 % 2 groups will make 200 sentences
        igroup
        target = [];
        noise = [];
        randNoise = randperm(25);
        for isent = 1:25
            if isent<10
                [ori,fs2]=audioread(['Speech_sound/BEL_SR/BEL_SR0' num2str(igroup) '0' num2str(isent) '.wav']);
            else
                [ori,fs2]=audioread(['Speech_sound/BEL_SR/BEL_SR0' num2str(igroup) num2str(isent) '.wav']);
            end
            target = [target;resample(ori,fs,fs2)];
            if randNoise(isent)<10
                [BB,fs2]=audioread(['Speech_sound/BEL_SR/BEL_SR0' num2str(igroup) '0' num2str(randNoise(isent)) '.wav']);
            else
                [BB,fs2]=audioread(['Speech_sound/BEL_SR/BEL_SR0' num2str(igroup) num2str(randNoise(isent)) '.wav']);
            end
            noise = [noise; BB];
        end
        noise = [noise; BB];
        noise_reversed = flipud(noise);

        % add spatial properties
        %1))) target
        Index = (Elevation - 1)*72+tar_index;%
        h_l = squeeze(out_hart(:,1,Index)); % Left HRIRs
        h_r = squeeze(out_hart(:,2,Index)); % Right HRIRs
        new_tar(:,1) = conv(target,h_l,'same');
        new_tar(:,2) = conv(target,h_r,'same');
        % figure;
        % subplot(221)
        % plot(h_l)
        % size(h_l)

        fsig = (1:length(h_l))./length(h_l).*fs;
        both_fft = fft(h_l);
        subplot(222);plot(fsig,abs(both_fft))
        xlim([0 fs/2])

        %2))) noise
        Index = (Elevation - 1)*72+noise_index;%
        h_l = squeeze(out_hart(:,1,Index)); % Left HRIRs
        h_r = squeeze(out_hart(:,2,Index)); % Right HRIRs
        new_noise(:,1) = conv(noise_reversed,h_l,'same');
        new_noise(:,2) = conv(noise_reversed,h_r,'same');
        Combined_dual = (new_tar+new_noise(1:length(target),:))./2; % /2 is to make it softer
        Combined_mono = sum(Combined_dual,2);
        % sound(Combined_mono(1:round(fs*4),:),fs) % play out the original noisy sound
        % pause(3)

        % ======== Processing
        % only do the noise
        if igroup==3
            % ifplot = 1;
            out_model=f_get_BB_model(h_l,h_r,ifplot,L_or_R);
        end
        % ifplot = 1;
        out=f_segregation(out_model.F_centers,Combined_dual,ifplot,L_or_R,soft_hard,rad);
        % out.freqs(1:200)
        processed_sound = out.reconstructed;
        % sound(processed_sound(1:round(fs*4),:),fs) % play out the processed sound
        % pause(4)
        audiowrite(['New_hard_sound\saved_Processed_TargetL_Group' num2str(igroup) '_Cond' num2str(Condition) '.wav'],processed_sound,fs)
        clear new_noise new_tar

        % pause
    end

end